package com.gkd.store.controllers;

import com.gkd.store.dao.AccountRepository;
import com.gkd.store.dao.UserRepository;
import com.gkd.store.entities.Account;
import com.gkd.store.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("register")
public class SecurityController {


    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
    AccountRepository accountRepository;
@Autowired
    UserRepository userRepository;

    @GetMapping("/new/account")
    public String registera(Model model){
        Account account = new Account();
        model.addAttribute("Accounts",account);
        return  "security/registerAccount";
    }
    @GetMapping("/new/user")
    public String registeru(Model model){
        User user = new User();
        model.addAttribute("User",user);
        return  "security/registerUser";
    }


    @PostMapping("/save/account")
    public String saveaccounts(Model model,Account Accounts){
        Accounts.setPassword(bCryptPasswordEncoder.encode(Accounts.getPassword()));
        accountRepository.save(Accounts);
        return "redirect:/login";
    }
    @PostMapping("/save/user")
    public String saveUser(Model model,User user){
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userRepository.save(user);
        return "redirect:/login";
    }
}

